package swd.maintain.project.sys_inc_prg_maintain;

import com.pub.database.*;
import javax.servlet.http.*;
import com.pub.web.*;

public class Action {
    QueryBean gqbnDAO = null;
    public String action(javax.servlet.jsp.PageContext pageContext) {
        HttpServletRequest request = (HttpServletRequest) pageContext.
                                     getRequest();
        HttpServletResponse response = (HttpServletResponse) pageContext.
                                       getResponse();

        View view = (View) request.getAttribute("view");
        (new WebFunction()).setPortal_to_session(request); //set session from portal
        if (request.getSession().getAttribute("u_id") == null) {
            try {
                request.getRequestDispatcher((new StringBuffer("login.jsp")).
                                             insert(0, "/").toString()).forward(
                        request, response);
                return "login";
            } catch (Exception le) {
                view.setOnloadScript(
                        "can not redirect to login page...please contact IT");
                return "";
            }
        }
        //String lstrUserNO = request.getSession().getAttribute("u_id").toString();
        gqbnDAO = new QueryBean(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());

        DataView DataView = null;
        DataViewForm DataViewForm = null;

        DataView = new DataView(view.getDBAlias(), view.getDBDebug(),
                                view.getSourceCharset(), view.getTargetCharset());
        DataViewForm = new DataViewForm();

        request.setAttribute("DataView", DataView); //讓前端可以呼叫dataview
        request.setAttribute("DataViewForm", DataViewForm);

        Business business = new Business(gqbnDAO);
        //Flow flow = new Flow(gqbnDAO, lstrUserNO, "SWD", view, business);

        String lstrResult = "";
        try {

            if (view.getprg_no() != null && view.getprg_no().length > 0) {
                view.setSelectShow("");
            } else {
                view.setSelectShow("none");
            }
            view.setButtonVisible("0");
            view.setins_dbf_no_readonly(true);
            view.setins_prg_name_e_readonly(true);
            view.setins_prg_no_readonly(true);
            view.setins_prg_name_readonly(true);

            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("delete")) {
                business.exeDelSQL(view, Integer.parseInt(view.getrowsn()));

            }
            // Save
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("save")) {
                //lstrResult = flow.addAction();
                view.setOnloadScript("alert('" + lstrResult + "');");
                if (lstrResult.equals("資料新增成功!!")) {
                    view.setButtonVisible("2");
                    view.setSelectShow("");
                    view.setins_dbf_no("");
                    view.setins_prg_name("");
                    view.setins_prg_name_e("");
                    view.setins_prg_no("");

                } else {
                    view.setButtonVisible("1");
                    view.setSelectShow("");
                    view.setins_dbf_no("");
                    view.setins_prg_name("");
                    view.setins_prg_name_e("");
                    view.setins_prg_no("");

                }
            }
            // Add
            if (view.getPrgAction() != null && view.getPrgAction().equals("add")) {
               // view.setAllClear();
                view.setins_dbf_no_readonly(false);
                view.setins_prg_name_e_readonly(false);
                view.setins_prg_no_readonly(false);
                view.setins_prg_name_readonly(false);
                view.setSelectShow("");
                view.setButtonVisible("1");
                view.setPrgStatus("add");
            }

            // 離開
            if (view.getPrgAction() != null &&
                view.getPrgAction().equals("exit")) {
                if (view.getPrgStatus() != null &&
                    (view.getPrgStatus().equals("add"))) {
                    view.setAllClear();
                    view.setButtonVisible("0");
                    view.setPrgStatus("exit");
                } else {
                    view.setAllClear();
                    view.setSelectShow("none");
                    view.setPrgStatus("exit");
                }
            }

            business.setDataViewForm(DataViewForm, view);
            return "";
        } catch (Exception e) {
            view.setOnloadScript("alert('Program error please contact IT " +
                                 e.toString() + "');");
            return "";
        } finally {
            try {
                if (gqbnDAO != null) {
                    gqbnDAO.close();
                }
            } catch (Exception e) {
                System.out.println("can not close querybean in Action.java");
            }
        }
    }
}
